// 路由配置
import {
	createRouter,
	createWebHistory,
	START_LOCATION
} from 'vue-router'
import Main from '../components/main/Main.vue'
import Home from '../components/home/Home.vue'
import Mine from '../components/mine/Mine.vue'
import About from '../components/about/About.vue'
import Login from '../components/user/login/Login.vue'
import Register from '../components/user/register/Register.vue'
import UserInfo from '../components/info/UserInfo.vue'
import Firends from '../components/firends/Firends.vue'
import AddFirends from '../components/firends/AddFirends.vue'
import Chat from '../components/chat/Chat.vue'

const routes = [{
		path: '/',
		redirect: '/home',
		name: "home",
		component: Main,
		children: [{
				path: 'home',
				name: "home",
				component: Home,
				meta: {
					title: "公司作业-消息列表"
				}
			},
			{
				path: 'firends',
				name: "firends",
				component: Firends,
				meta: {
					title: "公司作业-好友列表"
				}
			},
			{
				path: 'mine',
				name: "mine",
				component: Mine,
				meta: {
					title: "公司作业-个人中心"
				}
			},
		]
	},
	{
		path: '/register',
		name: "register",
		component: Register,
		meta: {
			title: "公司作业-注册"
		}
	},
	{
		path: '/login',
		name: "login",
		component: Login,
		meta: {
			title: "公司作业-登录"
		}
	},
	{
		path: '/userinfo',
		name: "userinfo",
		component: UserInfo,
		meta: {
			title: "公司作业-我的信息"
		}
	},
	{
		path: '/addfriends',
		name: "addfriends",
		component: AddFirends,
		meta: {
			title: "公司作业-添加好友"
		}
	},
	{
		path: '/chat',
		name: "chat",
		component: Chat,
		meta: {
			title: "公司作业-聊天界面"
		}
	},
	{
		path: '/about',
		name: "about",
		component: About,
		meta: {
			title: "公司作业-关于聊天"
		}
	},
]

const router = createRouter({
	// 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
	history: createWebHistory(),
	routes, // `routes: routes` 的缩写
})

router.beforeEach((to, from, next) => {
	console.log("before:from " + from.path)
	console.log("before:to " + to.path)
	if (from === START_LOCATION) {
		// initial navigation
	}
	if (to.meta.title) { //判断是否有标题
		if (to.path == "/chat") {
			let firend = JSON.parse(to.query.firend)
			console.log("将要打开与 " + firend.account + " 的聊天界面")
			document.title = to.meta.title + " " +firend.account
		} else {
			document.title = to.meta.title
		}
	}
	next()
})

export default router